/**
 * 产品选择
 * @param callBack
 * @returns {Boolean}
 */
function getChooseProductinfos(callBack){
	var stockName = '';
	
	$.ajax({
        type: "POST",
        url: contextPath + "/getChooseProductinfo",
        dataType: "html",
        data: {'stockName': stockName},
        beforeSend: function () {
            $("#beforeLoading").show();
            timeOutHint();
        },
        success: function (res) {
            if ("" != res) {
                $("#beforeLoading").hide();
                
                // 详情窗口
                var mx = mx || $.Mw({
        			titleText : '选择产品：',
        			contentText : res,
        			confirmBtnText : '关闭',
        			width : 600,
        			isDisposeWhileClose : true

        		})
        		.confirmOnly(true)
        		.hideConfirmButton()
        		.addLink('', function(){
		             	//保存代码
		            	var selected = $('#productinfos>table>tbody .ctr-checkbox:checked');
		             	
						if (callBack && typeof callBack === 'function') {
							
							var dataStocks = [];
							var dstock = null;
							$.each(selected, function(){
								dstock = {};
								dstock.id = $(this).attr("data-id");
								dstock.productName = $(this).attr("data-productName");
								dstock.attribute = $(this).attr("data-attribute");
								dstock.brandName = $(this).attr("data-brandName");
								dstock.productinfoAuantity = $(this).attr("data-productinfoAuantity");
								dstock.unit = $(this).attr("data-unit");
								dstock.productinfoCost = $(this).attr("data-productinfoCost");
								dstock.qs = $(this).attr("data-qs");
								dstock.productinfoStatus = $(this).attr("data-productinfoStatus");
								dstock.serialNumber = $(this).attr("data-serialNumber");
								
								dataStocks.push(dstock);
								
							});
							
							
							callBack(dataStocks);
						}
						
		             	//重置表单
	                	 mx.close();
		         		}, {'class':'fa fa-check-circle OKsubmit'})
		         .addLink('', function(){
		            	//取消
		            	mx.close();
		            	}, {'class':'fa fa-close NOsubmit'});;

        		mx.open();
        	
            }
        },
        error: function(){
        	$("#beforeLoading").hide();
        	failHint();
        }
    });
}




/**
 * 库存产品选择
 * @param callBack
 * @returns {Boolean}
 */
function getChooseStocks(callBack, flag){
	var stockName = '';
	
	if(1==flag){
		var outstockName = $('#outStock').find("option:selected").val();
		var instockName = $('#inStock').find("option:selected").val();
		
		if(undefined!=outstockName && undefined!=instockName && outstockName==instockName){
			failHint("同仓库无需调拨");
			return false;
		}
		
		stockName = outstockName;
	}
	else if(2==flag){
		stockName = $('#stockName').find("option:selected").val();
	}
	else if(3==flag){
		stockName = $('#stockName').find("option:selected").val();
	}
	else if(8==flag || 9==flag || 10==flag || 11==flag || 12==flag || 13==flag || 14==flag || 15==flag){
		stockName = $('#stockName').find("option:selected").val();
	}
		
	
	$.ajax({
        type: "POST",
        url: contextPath + "/getChooseStocks",
        dataType: "html",
        data: {'stockName': stockName},
        beforeSend: function () {
            $("#beforeLoading").show();
            timeOutHint();
        },
        success: function (res) {
            if ("" != res) {
                $("#beforeLoading").hide();
                
                // 详情窗口
                var mx = mx || $.Mw({
        			titleText : '选择库存产品：',
        			contentText : res,
        			confirmBtnText : '关闭',
        			width : 600,
        			isDisposeWhileClose : true

        		})
        		.confirmOnly(true)
        		.hideConfirmButton()
        		.addLink('', function(){
		             	//保存代码
		            	var selected = $('#productinfos>table>tbody .ctr-checkbox:checked');
		             	
						if (callBack && typeof callBack === 'function') {
							
							var dataStocks = [];
							var dstock = null;
							$.each(selected, function(){
								dstock = {};
								dstock.id = $(this).attr("data-id");
								dstock.productName = $(this).attr("data-productName");
								dstock.attribute = $(this).attr("data-attribute");
								dstock.brandName = $(this).attr("data-brandName");
								dstock.productinfoAuantity = $(this).attr("data-productinfoAuantity");
								dstock.unit = $(this).attr("data-unit");
								dstock.productinfoCost = $(this).attr("data-productinfoCost");
								dstock.qs = $(this).attr("data-qs");
								dstock.productinfoStatus = $(this).attr("data-productinfoStatus");
								dstock.serialNumber = $(this).attr("data-serialNumber");
								dstock.productinfoNewTradePrice = $(this).attr("data-productinfoNewTradePrice");
								
								dataStocks.push(dstock);
								
							});
							
							
							callBack(dataStocks);
						}
						
		             	//重置表单
	                	 mx.close();
		         		}, {'class':'fa fa-check-circle OKsubmit'})
		         .addLink('', function(){
		            	//取消
		            	mx.close();
		            	}, {'class':'fa fa-close NOsubmit'});;

        		mx.open();
        	
            }
        },
        error: function(){
        	$("#beforeLoading").hide();
        	failHint();
        }
    });
}


/**
 * 经手人选择
 * @param callBack
 * @returns {Boolean}
 */
function getChooseContacts(callBack, flag){
	
	$.ajax({
        type: "POST",
        url: contextPath + "/getChooseContacts",
        dataType: "html",
        data: {'flag': flag},
        beforeSend: function () {
            $("#beforeLoading").show();
            timeOutHint();
        },
        success: function (res) {
            if ("" != res) {
                $("#beforeLoading").hide();
                
                // 详情窗口
                var mx = mx || $.Mw({
        			titleText : '选择经手人：',
        			contentText : res,
        			confirmBtnText : '关闭',
        			width : 600,
        			isDisposeWhileClose : true

        		})
        		.confirmOnly(true)
        		.hideConfirmButton()
        		.addLink('', function(){
		             	//保存代码
		            	var selected = $('#contactsChoose>table>tbody .ctr-checkbox:checked');
		             	
						if (callBack && typeof callBack === 'function') {
							
							if(1==flag){
								var datas = [];
								var data = null;
								$.each(selected, function(){
									data = {};
									data.id = $(this).attr("data-id");
									data.name = $(this).attr("data-name");
									data.uType = $(this).attr("data-uType");
									data.campany = $(this).attr("data-campany");
									data.qs = $(this).attr("data-qs");
									
									datas.push(data);
									
								});
								
								
								callBack(datas);
							}
							else{
								var data = {};
									data.id = $(selected).attr("data-id");
									data.name = $(selected).attr("data-name");
									data.uType = $(selected).attr("data-uType");
									data.campany = $(selected).attr("data-campany");
									data.qs = $(selected).attr("data-qs");
									
								callBack(data);
							}
							
							
						}
						
		             	//重置表单
	                	 mx.close();
		         		}, {'class':'fa fa-check-circle OKsubmit'})
		         .addLink('', function(){
		            	//取消
		            	mx.close();
		            	}, {'class':'fa fa-close NOsubmit'});;

        		mx.open();
        	
            }
        },
        error: function(){
        	$("#beforeLoading").hide();
        	failHint();
        }
    });
}


/**
 * 经手人选择
 * @param callBack
 * @returns {Boolean}
 */
function getChooseContacts(callBack, flag, approvalGrade){
	
	$.ajax({
        type: "POST",
        url: contextPath + "/getChooseContacts",
        dataType: "html",
        data: {'flag': flag},
        beforeSend: function () {
            $("#beforeLoading").show();
            timeOutHint();
        },
        success: function (res) {
            if ("" != res) {
                $("#beforeLoading").hide();
                
                // 详情窗口
                var mx = mx || $.Mw({
        			titleText : '选择经手人：',
        			contentText : res,
        			confirmBtnText : '关闭',
        			width : 600,
        			isDisposeWhileClose : true

        		})
        		.confirmOnly(true)
        		.hideConfirmButton()
        		.addLink('', function(){
		             	//保存代码
		            	var selected = $('#contactsChoose>table>tbody .ctr-checkbox:checked');
		             	
						if (callBack && typeof callBack === 'function') {
							
							if(1==flag){
								var datas = [];
								var data = null;
								$.each(selected, function(){
									data = {};
									data.id = $(this).attr("data-id");
									data.name = $(this).attr("data-name");
									data.uType = $(this).attr("data-uType");
									data.campany = $(this).attr("data-campany");
									data.qs = $(this).attr("data-qs");
									
									datas.push(data);
									
								});
								
								
								callBack(datas, approvalGrade);
							}
							else{
								var data = {};
									data.id = $(selected).attr("data-id");
									data.name = $(selected).attr("data-name");
									data.uType = $(selected).attr("data-uType");
									data.campany = $(selected).attr("data-campany");
									data.qs = $(selected).attr("data-qs");
									
								callBack(data, approvalGrade);
							}
							
							
						}
						
		             	//重置表单
	                	 mx.close();
		         		}, {'class':'fa fa-check-circle OKsubmit'})
		         .addLink('', function(){
		            	//取消
		            	mx.close();
		            	}, {'class':'fa fa-close NOsubmit'});;

        		mx.open();
        	
            }
        },
        error: function(){
        	$("#beforeLoading").hide();
        	failHint();
        }
    });
}




/**
 * 客户选择
 * @param callBack
 * @returns {Boolean}
 */
function getChooseCompany(callBack, flag){
	
	$.ajax({
        type: "POST",
        url: contextPath + "/getChooseCompany",
        dataType: "html",
        data: {'isvalidity': 1, 'isEnabled': 0, 'flag': flag},
	    beforeSend: function () {
	        $("#beforeLoading").show();
	        timeOutHint();
	    },
	    success: function (res) {
	        if ("" != res) {
	            $("#beforeLoading").hide();
	            
	            // 详情窗口
	            var mx = mx || $.Mw({
	    			titleText : '选择客户：',
	    			contentText : res,
	    			confirmBtnText : '关闭',
	    			width : 600,
	    			isDisposeWhileClose : true
	
	    		})
	    		.confirmOnly(true)
	    		.hideConfirmButton()
	    		.addLink('', function(){
		             	//保存代码
		            	var selected = $('#contactsChoose>table>tbody .ctr-checkbox:checked');
		             	
						if (callBack && typeof callBack === 'function') {
							
							if(1==flag){
								var datas = [];
								var data = null;
								$.each(selected, function(){
									data = {};
									data.id = $(this).attr("data-id");
									data.companyName = $(this).attr("data-companyName");
									data.companyType = $(this).attr("data-companyType");
									data.categoryName = $(this).attr("data-categoryName");
									data.contact = $(this).attr("data-contact");
									data.contactTel = $(this).attr("data-contactTel");
									data.qs = $(this).attr("data-qs");
									
									datas.push(data);
									
								});
								
								
								callBack(datas, 1);
							}
							else{
								var data = {};
									data.id = $(selected).attr("data-id");
									data.companyName = $(selected).attr("data-companyName");
									data.companyType = $(selected).attr("data-companyType");
									data.categoryName = $(selected).attr("data-categoryName");
									data.contact = $(selected).attr("data-contact");
									data.contactTel = $(selected).attr("data-contactTel");
									data.qs = $(selected).attr("data-qs");
									
								callBack(data, 0);
							}
							
							
						}
						
		             	//重置表单
	                	 mx.close();
		         		}, {'class':'fa fa-check-circle OKsubmit'})
		         .addLink('', function(){
		            	//取消
		            	mx.close();
		            	}, {'class':'fa fa-close NOsubmit'});;
	
	    		mx.open();
	    	
	        }
	    },
	    error: function(){
	    	$("#beforeLoading").hide();
	    	failHint();
	    }
	});
}



/**
 * 敬请期待
 */
function shownotice() {
	
    $.Mw({
        contentText: '<i class="fa fa-exclamation-circle tips-error"></i><div>功能开发中，敬请期待！</div>',
        isTips: true,
        central: true,
        autoClose: 2000

    }).disposeWhileClose().open();

}



/**
 * 操作失败提示
 */
function failHint(msg){
	$("#beforeLoading").hide();
	$.Mw({
		titleText: '提示',
		contentText: msg ? '<i class="fa fa-exclamation-circle tips-error"></i><div>'+msg+'</div>' : '<i class="fa fa-exclamation-circle tips-error"></i><div>操作失败</div>',
		theme: 'glass',
		central: true,
		width: 260,
		hideTools: true,
		isTips: true,
		autoClose: 2500,
		effect: 'slide'
	}).open()
	.confirmOnly(true)
	.hideConfirmButton()
	.disposeWhileClose();
}

/**
 * 操作成功提示
 */
function succHint(msg){
	$("#beforeLoading").hide();
	$.Mw({
		titleText: '提示',
		contentText: msg ? '<i class="fa fa-exclamation-circle tips-error"></i><div>'+msg+'</div>' : '<i class="fa fa-exclamation-circle tips-error"></i><div>操作成功</div>',
		theme: 'glass',
		central: true,
		width: 260,
		hideTools: true,
		isTips: true,
		autoClose: 2500,
		effect: 'slide'
	}).open()
	.confirmOnly(true)
	.hideConfirmButton()
	.disposeWhileClose();
}


/**
 * 错误提示
 */
function errorHint(msg){
	$.Mw({
		titleText: '提示',
		contentText: '<i class="fa fa-times-circle-o tips-success"></i><div>'+msg+'</div>',
		theme: 'glass',
		central: true,
		width: 260,
		hideTools: true,
		isTips: true,
		autoClose: 2500,
		effect: 'slide',
	}).open()
	.confirmOnly(true)
	.hideConfirmButton()
	.disposeWhileClose();
}



/**
 * 确认框提示
 */
function showConfirm(msg,callBack) {

	$.Mw({
		contentText: '<i class="fa fa-exclamation-circle tips-success"></i><div>'+msg+'</div>',
		central: true,
		theme: 'alert'
	}).disposeWhileClose()
		.confirmOnly(true)
		.hideConfirmButton()
		.addLink("",function() {
			this.close();
			callBack();
		}, { 'class': 'fa fa-check-circle OKsubmit OKsubmitwhite' })
		.addLink('', function () {
			//取消
			this.close();

		}, { 'class': 'fa fa-close NOsubmit NOsubmitwhite' })
		.open();

}



var hintMw;
var autoOFF = true;

/**
 * 加载提示
 * @param msg
 */
function loadingPage(msg){
	hintMw = $.Mw({
		titleText: '提示',
		contentText: msg ? '<i class="fa fa-spinner fa-pulse tips-error" id="hintIcon"></i><div>'+msg+'</div>' : '<i class="fa fa-spinner fa-pulse tips-error" id="hintIcon"></i><div id="hintTitle">正在加载...</div>',
		theme: 'tips',
		central: true,
		width: 260,
		hideTools: true,
		effect: 'fast'
	}).open()
	.confirmOnly(true)
	.hideConfirmButton()
	.disposeWhileClose();
	
	
	setTimeout(function () {
		
		if(autoOFF){
			
			autoOFF = false;
			
			$("#hintTitle").text("数据服务异常...");
			
			hintMw.autoClose(2500);
			
			window.location.reload(true);
			
		}
		
	}, 30000);
	
}


/**
 * 提示窗口转换-->succHint
 */
function loadingToSuccHint(){
	autoOFF = false;
	$("#hintIcon").removeClass("fa-spinner").removeClass("fa-pulse").addClass("fa-exclamation-circle");
	$("#hintTitle").text("操作成功");
	
	hintMw.autoClose(2500).open();
	
	hintMw = null;
}


/**
 * 提示窗口转换-->failHint
 */
function loadingToFailHint(msg){
	autoOFF = false;
	$("#hintIcon").removeClass("fa-spinner").removeClass("fa-pulse").addClass("fa-exclamation-circle");
	if(null!=msg && ""!=msg){
		$("#hintTitle").text(msg);
	}
	else{
		$("#hintTitle").text("操作失败");
	}
	
	hintMw.autoClose(2500).open();
	
	hintMw = null;
}



/**
 * 无权操作提示
 */
function rolesErrorHint(msg){
	if(hintMw){
		hintMw.close();
	}
	hintMw = $.Mw({
		titleText: '提示',
		contentText: msg ? '<i class="fa fa-exclamation-circle tips-error"></i><div>'+msg+'</div>' : '<i class="fa fa-exclamation-circle tips-error"></i><div>非常抱歉，你无权访问此页</div>',
		theme: 'glass',
		central: true,
		width: 260,
		hideTools: true,
		isTips: true,
		autoClose: 2500,
		effect: 'slide'
	}).open()
	.confirmOnly(true)
	.hideConfirmButton()
	.disposeWhileClose();
	
}


/**
 * F5刷新是否离开本页
 *
 * @param url 需要重定向的url，可以为空
 */
function f5Confirm(url){
	var mxc;

	var div = $("<div id='success-tip' style='display:none'><i class='fa fa-exclamation-circle tips-success'></i><div>是否要离开本页面</div></div>")

	/**********是否离开本页 开始**********/
	document.onkeydown = function (e) {
		e = e || event;
		if (e.keyCode == 116) {  //判断是否单击的enter按键(回车键)
			mxc = mxc || $.Mw(
				{
					titleText: '',
					contentjQuery: div,
					confirmBtnText: '关闭',
					width: 300,
					central:true,
					noTools:false,
					top: 200,
					theme:'alert'

				}).confirmOnly(true)
				.hideConfirmButton()
				.addLink('', function(){
					
					
					/////////////////////////////////////////////
					
					
					$.ajax({
			            type: "POST",
			            url: contextPath + "/sessionTimeOutF5",
			            data: {'oldpwd': $("#oldpwd").val(), 'password': $("#password").val()},
			            dataType : "json",
			            beforeSend : function(){
			            	$("#beforeLoading").show();
			            },
			            success: function (res) {
			            	$("#beforeLoading").hide();
			            	if(!res.status){
			            		
		                		var _url = contextPath + "/exit";
		                		SessionOutTimeHint(_url);
			            		
			            	}
			            	else{
			            		if(url){
									window.location.href= url;
								}
								else{
									window.location.reload(true);
								}
			            	}
			            	
			            	
			            }
			        });
					
					
					////////////////////////////////////////////
					
					
					

					//重置表单
					mxc.close();
				}, {'class':'fa fa-check-circle OKsubmit OKsubmitwhite'})
				.addLink('', function(){
					//取消
					mxc.close();

				}, {'class':'fa fa-close NOsubmit NOsubmitwhite'});
			mxc.open();
			event.keyCode = 0;
			event.cancelBubble = true;
			return false;
		}
	}
	/**********是否离开本页 结束**********/
}


/**
 * 链接超时提示
 */
function SessionOutTimeHint(url){
	if(hintMw){
		hintMw.close();
	}
	hintMw = $.Mw({
		titleText: '提示',
		contentText: '<i class="fa fa-exclamation-circle tips-error"></i><div>Session超时，请重新登录...</div>',
		theme: 'glass',
		central: true,
		width: 260,
		hideTools: true,
		isTips: true,
		autoClose: 2500,
		effect: 'slide'
	}).open()
	.confirmOnly(true)
	.hideConfirmButton()
	.disposeWhileClose();
	
	
	setTimeout(function () {
		parent.location.href = url;
	}, 2500);
}


/**
 * 页面转向提示
 */
function changeHintToPage(callback, msg){
	if(hintMw){
		hintMw.close();
	}
	
	hintMw = $.Mw({
		titleText: '提示',
		contentText: '<i class="fa fa-exclamation-circle tips-error"></i><div>'+msg+'</div>',
		theme: 'glass',
		central: true,
		width: 260,
		hideTools: true,
		isTips: true,
		autoClose: 2500,
		effect: 'slide'
	}).open()
	.confirmOnly(true)
	.hideConfirmButton()
	.disposeWhileClose();
	
	
	setTimeout(function () {
		eval(callback+"()");
	}, 2500);
}


/**
 * 链接超时提示内容
 */
function timeOutHintContent(){
	if(hintMw){
		hintMw.close();
	}
	hintMw = $.Mw({
		titleText: '提示',
		contentText: '<i class="fa fa-exclamation-circle tips-error"></i><div>连接超时<br>请重试</div>',
		theme: 'glass',
		central: true,
		width: 260,
		hideTools: true,
		isTips: true,
		autoClose: 3000,
		effect: 'slide'
	}).open()
	.confirmOnly(true)
	.hideConfirmButton()
	.disposeWhileClose();
}


var timeoutHint;

/**
 * 链接超时提示
 */
function timeOutHint(second){
	var n = 25;
	
	if(null!=second && ''!=second){
		n = second;
	}
	
	clearTimeout(timeoutHint);
	
	timeoutHint = setTimeout(function () {
    	
    	var display = $("#beforeLoading").css('display');
    	
    	if('none'!=display){
    		$("#beforeLoading").hide();
    		timeOutHintContent();
    	}
    	
	}, n * 1000);
}



/**
 * 区域
 * @param targetId
 * @param id
 */
function areasChange(targetId, id){
	
	$.ajax({
        type: "POST",
        url: contextPath + "/getAreaChilds",
        data: {'id': id},
        dataType : "json",
        beforeSend : function(){
        	$("#beforeLoading").show();
        },
        success: function (res) {
        	$("#beforeLoading").hide();
        	
        	if(res.status){
        		var areas = res.areas;
        		var options = '';
        		
        		for(var i=0;i<areas.length;i++){
        			if(''==options){
        				options = '<option value="'+areas[i].id+'">'+areas[i].divisionName+'</option>';
        			}
        			else{
        				options = options + '<option value="'+areas[i].id+'">'+areas[i].divisionName+'</option>';
        			}
        		}
        		
        		$('#'+targetId+'').html(options);
        		
        	}
        	
        },
        error: function (msg) {
        	failHint();
        }
    });
}


// 打印
function printArea(id){
	$('#'+id+'').jqprint({
	     debug: false, 			//如果是true则可以显示iframe查看效果（iframe默认高和宽都很小，可以再源码中调大），默认是false
	     importCSS: true, 		//true表示引进原来的页面的css，默认是true。（如果是true，先会找$("link[media=print]")，若没有会去找$("link")中的css文件）
	     printContainer: true, 	//表示如果原来选择的对象必须被纳入打印（注意：设置为false可能会打破你的CSS规则）。
	     operaSupport: true		//表示如果插件也必须支持歌opera浏览器，在这种情况下，它提供了建立一个临时的打印选项卡。默认是true
	});
}


/**
 * 打印预览
 * @param callBack
 * @returns {Boolean}
 */
function getPrintPage(id){
	
	$.ajax({
        type: "POST",
        url: contextPath + "/getPrintStockDetailBills",
        dataType: "html",
        data: {'id': id},
        beforeSend: function () {
            $("#beforeLoading").show();
            timeOutHint();
        },
        success: function (res) {
            if ("" != res) {
                $("#beforeLoading").hide();
                
                // 详情窗口
                var mx = mx || $.Mw({
        			titleText : '打印预览',
        			contentText : res,
        			confirmBtnText : '关闭',
        			width : '800',
        			height : '95.5%',
        			isDisposeWhileClose : true

        		})
        		.confirmOnly(true)
        		.hideConfirmButton();
        		
        		mx.open();
        		
        		$('.mw-title').parent().css({'top':'0'});
        		$('.mw-title').parent().next().css({'top':'0'});
        		$('.mw-content').attr("style", "height: 80% !important");
            }
        },
        error: function(){
        	$("#beforeLoading").hide();
        	failHint();
        }
    });
}

